package com.ruoyi.buss.expand.service.impl;

import java.util.List;

import com.ruoyi.buss.customer.domain.DataCustomerInfo;
import com.ruoyi.buss.customer.mapper.DataCustomerInfoMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.expand.mapper.DataCustExpandMapper;
import com.ruoyi.buss.expand.domain.DataCustExpand;
import com.ruoyi.buss.expand.service.IDataCustExpandService;

/**
 * 跟进拓展信息Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class DataCustExpandServiceImpl implements IDataCustExpandService 
{
    @Autowired
    private DataCustExpandMapper dataCustExpandMapper;

    @Autowired
    private DataCustomerInfoMapper dataCustomerInfoMapper;

    /**
     * 查询跟进拓展信息
     * 
     * @param id 跟进拓展信息ID
     * @return 跟进拓展信息
     */
    @Override
    public DataCustExpand selectDataCustExpandById(String id)
    {
        return dataCustExpandMapper.selectDataCustExpandById(id);
    }

    /**
     * 查询跟进拓展信息列表
     * 
     * @param dataCustExpand 跟进拓展信息
     * @return 跟进拓展信息
     */
    @Override
    public List<DataCustExpand> selectDataCustExpandList(DataCustExpand dataCustExpand, LoginUser userInfo)
    {
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        // 如果是admin用户不做处理
        if (userInfo.getRoles().contains("zhhz")) {
            dataCustExpand.setOrgId(orgId);
        } else if (userInfo.getRoles().contains("common")) {
            dataCustExpand.setOrgId(orgId);
            dataCustExpand.setUserId(userId);
        }

        return dataCustExpandMapper.selectDataCustExpandList(dataCustExpand);
    }

    /**
     * 新增跟进拓展信息
     * 
     * @param dataCustExpand 跟进拓展信息
     * @return 结果
     */
    @Override
    public int insertDataCustExpand(DataCustExpand dataCustExpand,LoginUser userInfo)
    {
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();

        //更新存量表中的标记
        DataCustomerInfo datacustomerinfo = dataCustomerInfoMapper.selectDataCustomerInfoByIdCard(dataCustExpand.getIdCard());
        datacustomerinfo.setIsExpand("1");
        dataCustomerInfoMapper.updateDataCustomerInfoByIdCard(datacustomerinfo);

        /**
         * 更新完成状态之后，再跟进拓展表中加入信息
         */
        dataCustExpand.setUserId(userId);
        dataCustExpand.setOrgId(orgId);

        dataCustExpand.setCreateTime(DateUtils.getNowDate());
        return dataCustExpandMapper.insertDataCustExpand(dataCustExpand);
    }

    /**
     * 修改跟进拓展信息
     * 
     * @param dataCustExpand 跟进拓展信息
     * @return 结果
     */
    @Override
    public int updateDataCustExpand(DataCustExpand dataCustExpand)
    {
        dataCustExpand.setUpdateTime(DateUtils.getNowDate());
        return dataCustExpandMapper.updateDataCustExpand(dataCustExpand);
    }

    /**
     * 批量删除跟进拓展信息
     * 
     * @param ids 需要删除的跟进拓展信息ID
     * @return 结果
     */
    @Override
    public int deleteDataCustExpandByIds(String[] ids)
    {
        return dataCustExpandMapper.deleteDataCustExpandByIds(ids);
    }

    /**
     * 删除跟进拓展信息信息
     * 
     * @param id 跟进拓展信息ID
     * @return 结果
     */
    @Override
    public int deleteDataCustExpandById(String id)
    {
        return dataCustExpandMapper.deleteDataCustExpandById(id);
    }

    @Override
    public int canelExpandCustomer(DataCustExpand dataCustExpand, LoginUser userInfo) {
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();

        //更新存量表中的标记
        DataCustomerInfo datacustomerinfo = dataCustomerInfoMapper.selectDataCustomerInfoByIdCard(dataCustExpand.getIdCard());
        datacustomerinfo.setIsExpand("0");
        dataCustomerInfoMapper.updateDataCustomerInfoByIdCard(datacustomerinfo);

        /**
         * 更新完成状态之后，再跟进拓展表中加入信息
         */
        dataCustExpand.setUserId(userId);
        dataCustExpand.setOrgId(orgId);

        dataCustExpand.setUpdateTime(DateUtils.getNowDate());
        return dataCustExpandMapper.canelExpandCustomer(dataCustExpand);
    }
}
